package com.atguigu.guli.service.trade.controller.api;

import com.atguigu.guli.service.base.helper.JwtHelper;
import com.atguigu.guli.service.base.result.R;
import com.atguigu.guli.service.trade.entity.Order;
import com.atguigu.guli.service.trade.service.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/api/trade/order")
@Api(description = "网站订单管理")
@Slf4j
public class ApiOrderController {
    @Autowired
    OrderService orderService;

    @ApiOperation("新增订单")
    @PostMapping("auth/save/{courseId}")
    public R save(@PathVariable String courseId,HttpServletRequest request ){
        String memberId = JwtHelper.getId(request);
        String orderId = orderService.saveOrder(courseId,memberId);

        return R.ok().data("orderId",orderId);
    }

    @ApiOperation("获取订单")
    @GetMapping("auth/getById/{orderId}")
    public R getById(@PathVariable String orderId,HttpServletRequest request){
        String memberId = JwtHelper.getId(request);
        Order order = orderService.getByOrderId(orderId,memberId);
        return R.ok().data("item",order);

    }

    @ApiOperation("判断课程是否购买")
    @GetMapping("auth/isBuy/{courseId}")
    public R isBuyByCourseId(@PathVariable String courseId,HttpServletRequest request){
        String memberId = JwtHelper.getId(request);
        Boolean isBuy = orderService.isBuyByCourseId(memberId,courseId);
        return R.ok().data("isBuy",isBuy);
    }

    @ApiOperation("查询订单的支付状态")
    @GetMapping("auth/getPayStatus/{orderId}")
    public R getPayStatus(@PathVariable String orderId){
        Order order = orderService.getById(orderId);
        return R.ok().data("status",order.getStatus()+"");

    }
}
